# -*- coding: utf-8 -*-
"""
@Time ： 2024/5/11 13:56
@Auth ： 李天天
"""
import os
import glob
import subprocess

from connect_to_device import *


# from adb_oper import _run_command_and_get_stout


def extract_apk_paths(directory):
    # 存储 apk 文件路径的列表
    apk_paths = []

    # 遍历目录
    for root, dirs, files in os.walk(directory):
        # 匹配 APK 文件
        apk_files = glob.glob(os.path.join(root, '*.apk'))
        # 将匹配到的 APK 文件路径添加到列表中
        apk_paths.extend(apk_files)

    return apk_paths


def uninstall_all_apks():
    for each in [d_one_device, d_two_device, d_three_device]:
    # for each in [d_three_device]:
        each.shell("pm uninstall com.arcvideo.ivi.sdk2")
        each.shell("pm uninstall com.arcvideo.totalmedia.cpdemo.Youku")
        each.shell("pm uninstall com.arcvideo.totalmedia.cpuidemo.IQIYI")
        each.shell("pm uninstall com.arcvideo.totalmedia.cpdemo.Tencent")
        each.shell("pm uninstall com.arcvideo.totalmedia.uidemo")


def stop_all_apks():
    for each in [d_one, d_two, d_three]:
        # each.app_stop("com.arcvideo.ivi.sdk2")
        each.app_stop("com.arcvideo.totalmedia.cpdemo.Youku")
        each.app_stop("com.arcvideo.totalmedia.cpuidemo.IQIYI")
        each.app_stop("com.arcvideo.totalmedia.cpdemo.Tencent")
        each.app_stop("com.arcvideo.totalmedia.uidemo")
        each.app_stop("com.arcvideo.ivi.sdk2")


def clear_all_apks():
    for each in [d_one, d_two, d_three]:
    # for each in [d_one]:
        # each.app_clear("com.arcvideo.ivi.sdk2")
        # each.app_clear("com.arcvideo.totalmedia.cpdemo.Youku")
        # each.app_clear("com.arcvideo.totalmedia.cpuidemo.IQIYI")
        # each.app_clear("com.arcvideo.totalmedia.cpdemo.Tencent")
        # each.app_clear("com.arcvideo.totalmedia.uidemo")
        each.app_clear("com.arcvideo.ivi.sdk2")



def install_all_apks_one_device():
    apks = extract_apk_paths(
        "/Users/only/Desktop/工作文档/DH/多屏/递交包/多屏中间件2.0提测APK_2024-08-27/台架版/合创前排")
    for apk in apks:
        d_one_device.install(apk, nolaunch=True)
    apks_sec = [each for each in apks if "SdkServer" not in each]
    print(apks_sec)
    for apk in apks_sec:
        adb_path = '/usr/local/bin/adb'
        command = f'{adb_path} -s {device_1} install --user 10 "{apk}"'
        print(command)
        try:
            result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
            print("命令执行成功:", result.stdout)
        except subprocess.CalledProcessError as e:
            print("命令执行失败:", e.stderr)


def install_all_apks_two_device():
    apks = extract_apk_paths(
        "/Users/only/Desktop/工作文档/DH/多屏/递交包/多屏中间件2.0提测APK_2024-08-27/台架版/合创后排")
    print(apks)
    for apk in apks:
        res = d_two_device.install(apk, nolaunch=True)
        print(res)


def install_all_apks_three_device():
    apks = extract_apk_paths(
        "/Users/only/Desktop/工作文档/DH/多屏/递交包/多屏中间件2.0提测APK_2024-08-27/移动版")
    print(apks)
    for apk in apks:
        res = d_three_device.install(apk, nolaunch=True)
        print(res)


# adb -s 10.10.113.54:5555 shell am start --user 10 --display 1 com.arcvideo.totalmedia.cpuidemo.IQIYI/com.arcvideo.totalmedia.cpuidemo.MainActivity
# adb -s 10.32.99.213:5555 shell am start --user 10 --display 1 com.arcvideo.totalmedia.cpuidemo.IQIYI/com.arcvideo.totalmedia.cpuidemo.MainActivity

if __name__ == '__main__':
    # res = extract_apk_paths(
    #     "/Users/only/Desktop/工作文档/DH/多屏/递交包/多屏中间件2.0提测APK_2024-05-11(台架一芯多屏版)/前排")
    # print(res)
    # uninstall_all_apks()
    # install_all_apks_one_device()
    # install_all_apks_three_device()
    # install_all_apks_two_device()
    stop_all_apks()
    clear_all_apks()
